import '../justfiles/go.just'

build: build-contracts copy-contract-artifacts build-go

test args='./...': build-contracts copy-contract-artifacts
    go test -v {{args}}

build-contracts:
  just ../packages/contracts-bedrock/build-no-tests

copy-contract-artifacts:
    #!/bin/bash
    set -euo pipefail

    rm -f ./pkg/deployer/artifacts/forge-artifacts/artifacts.tzst
    go run ./pkg/deployer/artifacts/cmd/mktar \
        -base ../packages/contracts-bedrock \
        -out ./pkg/deployer/artifacts/forge-artifacts/artifacts.tzst \
        --exclude="*.t.sol"

_LDFLAGSSTRING := "'" + trim(
    "-X github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/version.GitCommit=" + GITCOMMIT + " " + \
    "-X github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/version.GitDate=" + GITDATE + " " + \
    "-X github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/version.Version=" + VERSION + " " + \
    "-X github.com/ethereum-optimism/optimism/op-deployer/pkg/deployer/version.Meta=" + VERSION_META + " " + \
    "") + "'"

build-go: (go_build "./bin/op-deployer" "./cmd/op-deployer" "-ldflags" _LDFLAGSSTRING)

docker-build: build-contracts copy-contract-artifacts
    #!/bin/bash
    cd ..
    export GIT_VERSION="untagged"
    export GIT_COMMIT=$(git rev-parse HEAD)
    export GIT_DATE=$(git show -s --format=%ct HEAD)
    docker buildx bake op-deployer --load --set op-deployer.tags=op-deployer:local

# Updates pkg/deployer/forge/version.json with version and checksum of tarball for each supported OS/arch
# - forge_version format: "v1.3.4"
update-forge forge_version="":
  #!/bin/bash
  ./scripts/forge-checksums.sh {{forge_version}}

check-forge-version:
  #!/bin/bash
  set -euo pipefail

  mise_version=$(yq '.tools.forge' ../mise.toml)
  echo "mise forge version       : $mise_version"
  op_deployer_version=$(jq -r '.forge' pkg/deployer/forge/version.json | sed 's/^v//')
  echo "op-deployer forge version: $op_deployer_version"

  if [ "$mise_version" != "$op_deployer_version" ]; then
    echo "ERROR: Forge versions don't match!"
    echo "  mise       : $mise_version"
    echo "  op-deployer: $op_deployer_version"
    exit 1
  fi

  echo -e "\n✓ forge versions match: $mise_version"

# ======================================
# Deployment and Verification Utilities
#
# The following commands are used to deploy the OPCM and the associated implementations contracts,
# they are meant to be run in the order which they are listed.
# This provides a linear flow of deployment and verification steps.
# ======================================

export ROOT_DIR := parent_directory(justfile_directory())
export DEFAULT_LOCATOR := "embedded"
export NETWORK := env_var_or_default("NETWORK", "sepolia")
export PROTOCOL_VERSIONS_PROXY := if NETWORK == "mainnet" { "0x8062AbC286f5e7D9428a0Ccb9AbD71e50d93b935" } else if NETWORK == "sepolia" { "0x79ADD5713B383DAa0a138d3C4780C7A1804a8090" } else { "" }
export SUPERCHAIN_CONFIG_PROXY := if NETWORK == "mainnet" { "0x95703e0982140D16f8ebA6d158FccEde42f04a4C" } else if NETWORK == "sepolia" { "0xC2Be75506d5724086DEB7245bd260Cc9753911Be" } else { "" }
export SUPERCHAIN_PROXY_ADMIN := if NETWORK == "mainnet" { "0x543ba4aadbab8f9025686bd03993043599c6fb04" } else if NETWORK == "sepolia" { "0x189abaaaa82dfc015a588a7dbad6f13b1d3485bc" } else { "" }
export L1_PROXY_ADMIN_OWNER := if NETWORK == "mainnet" { "0x5a0Aae59D09fccBdDb6C6CcEB07B7279367C3d2A" } else if NETWORK == "sepolia" { "0x1Eb2fFc903729a0F03966B917003800b145F56E2" } else { "" }
export CHALLENGER := if NETWORK == "mainnet" { "0x9BA6e03D8B90dE867373Db8cF1A58d2F7F006b3A" } else if NETWORK == "sepolia" { "0xfd1D2e729aE8eEe2E146c033bf4400fE75284301" } else { "" }

# This command should be called before any deployment or verification commands.
_validate_rpc:
  #!/bin/bash
  CHAIN_ID=$(cast chain-id)
  EXPECTED_CHAIN_ID=$(if [ "$NETWORK" == "mainnet" ]; then echo "1"; elif [ "$NETWORK" == "sepolia" ]; then echo "11155111"; else echo ""; fi)
  if [ "$CHAIN_ID" != "$EXPECTED_CHAIN_ID" ]; then
    echo "Error: RPC chain ID $CHAIN_ID does not match expected chain ID $EXPECTED_CHAIN_ID for network $NETWORK"
    exit 1
  fi

# Run this before deploying the OPCM.
pre-deploy: build build-contracts

# Run with:
# PRIVATE_KEY=0x1234.... NETWORK=sepolia just deploy-opcm
deploy-opcm locator="$DEFAULT_LOCATOR": _validate_rpc
  #!/bin/bash
  echo "Using artifacts locator: {{locator}}"
  go run ./cmd/op-deployer bootstrap implementations \
    --l1-rpc-url $ETH_RPC_URL \
    --private-key $PRIVATE_KEY \
    --protocol-versions-proxy $PROTOCOL_VERSIONS_PROXY \
    --superchain-config-proxy $SUPERCHAIN_CONFIG_PROXY \
    --upgrade-controller $L1_PROXY_ADMIN_OWNER \
    --superchain-proxy-admin $SUPERCHAIN_PROXY_ADMIN \
    --challenger $CHALLENGER \
    --outfile deploy-${NETWORK}.json

# Verifies the OPCM and the associated implementations contracts on Etherscan.
# This should be run after the OPCM is deployed, and requires the deploy-$NETWORK.json file.
verify-opcm locator="$DEFAULT_LOCATOR": _validate_rpc
  #!/bin/bash
  echo "Using artifacts locator: {{locator}}"
  go run ./cmd/op-deployer verify \
    --artifacts-locator {{locator}} \
    --l1-rpc-url $ETH_RPC_URL \
    --etherscan-api-key $ETHERSCAN_API_KEY \
    --input-file deploy-${NETWORK}.json
